package com.mfnote.adapter;

import java.util.List;

import com.mfnote.model.Item;
import com.mfnote.view.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * 记事详情中ExpandListView适配器
 * @author ZHF
 *
 */
public class ExpandAdapter extends BaseExpandableListAdapter{
	
	private Context mContext;
	private LayoutInflater mInflater = null;
	private String[] mGroupStrings = null; //分组名称：（今天，本周，更早）
	private List<List<Item>> mData = null;
	
	public ExpandAdapter(Context context, List<List<Item>> list) {
		this.mContext = context;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mGroupStrings = mContext.getResources().getStringArray(R.array.groups); //分组名称
		mData = list;
	}
	
	public void setData(List<List<Item>> list) {
		mData = list;
	}
	
	/***返回在指定Group的Child数目。*/
	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}

	/**获取子组中数量**/
	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return mData.get(groupPosition).size();
	}

	/**获取分组中的数据**/
	@Override
	public List<Item> getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return mData.get(groupPosition);
	}

	/**获取子组中的数据**/
	@Override
	public Item getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return mData.get(groupPosition).get(childPosition);
	}

	/**获取分组的Id,且独一无二**/
	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}
	/**获取子组的Id,且独一无二**/
	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		if(convertView == null) {
			convertView = mInflater.inflate(R.layout.fragment_detail_group_item_layout, null);
		}
		GroupViewHolder holder = new GroupViewHolder();
		holder.mGroupName = (TextView) convertView.findViewById(R.id.group_name);
System.out.println("------->适配器中当前组名："+groupPosition);
		holder.mGroupName.setText(mGroupStrings[groupPosition]); //组名
		holder.mGroupCount = (TextView) convertView.findViewById(R.id.group_count);
		holder.mGroupCount.setText("[" + mData.get(groupPosition).size() + "]"); //组员数量
		
		holder.mGroup_iv_floder = (ImageView) convertView.findViewById(R.id.group_iv_floder);
		if(isExpanded) {
			holder.mGroup_iv_floder.setBackgroundResource(R.drawable.drawable_expand_open);
		}else {
			holder.mGroup_iv_floder.setBackgroundResource(R.drawable.drawable_expand_close);
		}
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if(convertView == null) {
			convertView = mInflater.inflate(R.layout.fragment_detail_child_item_layout, null);
		}
		ChildViewHolder holder = new ChildViewHolder();
		//设置每一个子项的图片
		holder.mIcon = (ImageView) convertView.findViewById(R.id.img);
		switch(getChild(groupPosition, childPosition).getImageId()) {
		case 0:
			holder.mIcon.setBackgroundResource(R.drawable.shortcut_notes); 
			break;
		case 1:
			holder.mIcon.setBackgroundResource(R.drawable.shortcut_paint); 
			break;
		case 2:
			holder.mIcon.setBackgroundResource(R.drawable.shortcut_hand); 
		   break;
		case 3:
			holder.mIcon.setBackgroundResource(R.drawable.shortcut_record); 
			break;
			
		}
		//设置每一个子项的标题
		holder.mChildName = (TextView) convertView.findViewById(R.id.item_name);
		holder.mChildName.setText(getChild(groupPosition, childPosition).getName());
		//设置每一个子项的详情
		holder.mDetail = (TextView) convertView.findViewById(R.id.item_time);
		holder.mDetail.setText(getChild(groupPosition, childPosition).getTime());
		
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		 /* 很重要：实现ChildView点击事件，必须返回true */  
		return true;
	}

	private class GroupViewHolder {
		TextView mGroupName;
		TextView mGroupCount;
		ImageView mGroup_iv_floder;
	}
	
	private class ChildViewHolder {
		ImageView mIcon;
		TextView mChildName;
		TextView mDetail;
	}
}
